-- Extra lua, contains some of my earliest code.
-- LUA_HOH works fine without this, this is just the gauge and the HUD.
addHook("PlayerThink", function(player)
if player.gauge == nil then	player.gauge = 100 return end-- The boost gauge variable. By default it is 100.
if player.prevrings == nil then	player.prevrings = 0 return end
	
	if player.gauge > 100 then -- Defines the max amount of boost the gauge can hold.
		player.gauge = 100 
	end
	if player.gauge >= 0 -- The minimum amount of boost the gauge can contain.
		if player.boosting then -- If the player boosts, it subtracts the gauge by 1 every 5 tics.
			if not (leveltime % 5)
				player.gauge = $-1
			end
		end
	else
		player.canboost = false
		player.boosting = false
	end

	--Auto-Regen
	if player.speed == 0 -- Player has to stand still for boost bar to charge
	and player.realtime > 3*TICRATE
		if not (leveltime % 10)--Slows charge to every 30 tics
			if player.gauge 
				or player.gauge == 0
				if player.gauge < 80 --Auto-regen limited to 80
					player.gauge = $+1 
				end
			end
		end
	end
	--Rings
	if player.rings > player.prevrings then
		player.gauge = $ + (player.rings - player.prevrings)*2
		player.prevrings = player.rings
	end
	if player.rings < player.prevrings then
		player.prevrings = player.rings
	end
end)
--Enemy Regen 
-- Thanks to Golden for this
local function mobjDamage (target, inflictor)
	if not inflictor or not inflictor.valid
	or not inflictor.player or not inflictor.player.valid then
		return
	end

	if target.flags & MF_ENEMY and target.health == 1 then
		inflictor.player.gauge = $ + 5
	end
end

addHook("MobjDamage", mobjDamage)
-- The HUD code, developed by MARIOMARIO13531.
-- Some of the code is from boss hud mod made by Inuyasha.
local function bost(v, player)
if player.mo.skin == "jasonfox" and player.mo.valid
	if (maptol & TOL_NIGHTS) or player.gauge == nil then -- Prevents the code from running in a Nights special stage
		return
	end 
	local boostfill = 0
	local boostcap = 0
	local p_bost = v.cachePatch("BOSTBAR")
	local p_emptey = v.cachePatch("NMID")
	local p_fill   = v.cachePatch("MID")
	local p_fstart = v.cachePatch("BEG")
	local p_estart = v.cachePatch("NBEG")
	local p_fend   = v.cachePatch("END")
	local p_eend   = v.cachePatch("NEND")
	local posx = 315
	local posy = 164

	boostcap = $ + 100
	boostfill = $ + player.gauge
    
	if player.mo.color != 0
	v.draw(210, 160, p_bost, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS, v.getColormap("sonic", skincolors[player.mo.color].invcolor))
	end
	posx = $ - 1
	for i = boostcap, 1, -1
		if boostfill >= i
			v.draw(posx, posy, p_fill, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS, v.getColormap("sonic", player.mo.color))
		else
			v.draw(posx, posy, p_emptey, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS)
		end
		posx = $ - 1
		if player.gauge >= 1
			v.draw(213, posy, p_fstart, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS, v.getColormap("sonic", player.mo.color))
		else 
			v.draw(213, posy, p_estart, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS)
		end
	
		if player.gauge == 100
			v.draw(315, posy, p_fend, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS, v.getColormap("sonic", player.mo.color))
		else
			v.draw(315, posy, p_eend, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_PERPLAYER|V_HUDTRANS)
		end	
	   end
	end
end
hud.add(bost, "game")

	
addHook("PlayerThink", function(player) --Super Form
if player.gauge == nil and player.powers[pw_super] > 0 then	player.gauge = 100 return end-- The boost gauge variable. By default it is 100.
if player.prevrings == nil and player.powers[pw_super] > 0 then	player.prevrings = 0 return end
	
	if player.gauge > 100 then -- Defines the max amount of boost the gauge can hold.
		player.gauge = 100 
	end
	if player.gauge >= 0 -- The minimum amount of boost the gauge can contain.
		if player.boosting then -- If the player boosts, it subtracts the gauge by 1 every 5 tics.
			if not (leveltime % 5)
				player.gauge = $-0
			end
		end
	else
		player.canboost = false
		player.boosting = false
	end

	--Auto-Regen Super
	if player.powers[pw_super] > 0
	and player.realtime > 3*TICRATE
		if not (leveltime % 30)--Slows charge to every 30 tics
			if player.gauge 
				or player.gauge == 0
				if player.gauge < 100 --Auto-regen limited to 80
					player.gauge = $+40 
				end
			end
		end
	end
	--Rings
	if player.rings > player.prevrings then
		player.gauge = $ + (player.rings - player.prevrings)*2
		player.prevrings = player.rings
	end
	if player.rings < player.prevrings then
		player.prevrings = player.rings
	end
end)